package offer;

public interface ComplexNumberMultiply {
    /**
     * A complex number can be represented as a string on the form "real+imaginaryi" where:
     *
     * real is the real part and is an integer in the range [-100, 100].
     * imaginary is the imaginary part and is an integer in the range [-100, 100].
     * i2 == -1.
     * Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications.
     *
     * @param num1      Complex number 1
     * @param num2      Complex number 2
     * @return          product
     */
    String complexNumberMultiply(String num1, String num2);
}
class ComplexNumberMultiplyTest{
    static final String TEST_01_NUM1 = "1+1i";
    static final String TEST_02_NUM2 = "1+1i";
    public static void main(String[] args) {
        new ComplexNumberMultiplyImpl1().complexNumberMultiply(TEST_01_NUM1, TEST_02_NUM2);
    }

}
/*
    仿真实现，定义一个复数类
 */
class ComplexNumberMultiplyImpl1 implements ComplexNumberMultiply{

    class ComplexNumber{
        private int real;
        private int imaginary;
        public ComplexNumber(){

        }
        public ComplexNumber(String num){
            String s1 = num.replace("i","");
            String[] parts = s1.split("\\+",0);
            this.real = Integer.parseInt(parts[0]);
            this.imaginary = Integer.parseInt(parts[1]);
        }
        public ComplexNumber multiply(ComplexNumber num){
            ComplexNumber ans = new ComplexNumber();
            ans.real = this.real * num.real - this.imaginary * num.imaginary;
            ans.imaginary = this.real * num.imaginary + this.imaginary * num.real;
            return ans;
        }

        @Override
        public String toString(){
            return this.real + "+" + this.imaginary +"i";
        }
    }

    @Override
    public String complexNumberMultiply(String num1, String num2) {
        ComplexNumber n1 = new ComplexNumber(num1);
        ComplexNumber n2 = new ComplexNumber(num2);
        ComplexNumber n3 = n1.multiply(n2);
        return n3.toString();
    }
}
